package com.wangpu.oa.service.impl;

import com.wangpu.oa.ao.RoleAo;
import com.wangpu.oa.dao.DaoFactory;
import com.wangpu.oa.dao.RoleDao;
import com.wangpu.oa.entity.RoleEntity;
import com.wangpu.oa.service.RoleService;
import com.wangpu.oa.util.UUID;

import static com.wangpu.oa.validate.AssertUtil.*;

import java.util.Date;
import java.util.List;

public class RoleServiceImpl implements RoleService{

	@Override
	public RoleEntity addRole(RoleAo ao,String currentUserId) throws Exception {
		String code=$("代号",ao.getCode());
		String name=$("名称",ao.getName());
		String remark=ao.getRemark();
		
		assertBetween("代号", code, 6, 32);
		assertMatch("代号只能是字母，数字，下划线", code, "\\w+");
		
		RoleDao rdao=DaoFactory.getDao(RoleDao.class);
		
		assertFalse("代号已存在", rdao.exists("code", code));
		assertBetween("名称", name, 2, 10);
		assertMatch("名称必须是中文，且不能超过10个", name, "[\\u4e00-\\u9fa5]{2,10}");
		
		RoleEntity r=new RoleEntity();
		r.setCode(code);
		r.setName(name);
		r.setRemark(remark);
		r.setId(UUID.random32());
		r.setCreateTime(new Date());
		r.setCreateUserId(currentUserId);
		
		
		rdao.insert(r);
		
		return r;
	}

	@Override
	public void deleteRole(String id) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public RoleEntity modifyRole(String id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RoleEntity> queryRole(String currentUserId) throws Exception {
		RoleDao rdao=DaoFactory.getDao(RoleDao.class);
		List<RoleEntity> rows=rdao.loadAll(currentUserId);
		return rows;
	}

}
